<?php

namespace App\Admin\Controllers;

use App\Models\User;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\InfoBox;
use Encore\Admin\Widgets\Table;
use Illuminate\Http\Request;

class TodayUserController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('今日新增用户')
            ->description('用户列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('详情')
            ->description('用户详情')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑')
            ->description('编辑用户')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新建')
            ->description('新建用户')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User);
        $grid->model()->whereDate('created_at', date('Y-m-d'));
        $grid->id('Id');
        $grid->name('用户名');
        $grid->phone('手机号');
        $grid->pid('推荐人')->display(function(){
            $data = User::where('id',$this->pid)->first();
            if($data){
                return $data->name;
            }else{
                return "<span class='label label-success'>创始人</span>";
            }
        });
        $grid->status('状态')->editable('select', [1 => '正常', 2 => '禁用']);
        $grid->register_code('邀请码');
        $grid->email('邮箱');
        
        $grid->created_at('注册时间');


        $grid->actions(function ($actions) {
            $actions->disableView();
            $actions->disableDelete();
            if ($actions->getKey() == 1) {
                $actions->disableEdit();
            }
            $id = $actions->getKey();
            $actions->append("<a href='/admin/subUsers/" . $actions->getKey() . "'>" . '<i class="fa fa-users"></i>' . "下属会员</a>");

        });
        return $grid;
    }
    //下级用户
    public function subUsers(Request $request, $id)
    {
        $user = User::findOrFail($id);
        $users = $user->subUsers();
        $headers = ['用户id', '用户名', '用户状态', '电话', '下属'];

        $ret = [];
        foreach ($users as $v) {
            if($v->status == 1){
                $status = '正常';
            }else{
                $status = '禁用';
            }
            $ret[] = [$v->id, $v->name, $status, $v->phone, "<a href='/admin/subUsers/" . $v->id . "'>" . '<i class="fa fa-users"></i>' . "下属会员</a>"];
        }

        $table = new Table($headers, $ret);

        $box = new Box('下级用户', $table->render());
        $box->style('info');
        $box->solid();

        $content = new Content();
        return $content
            ->header('下级用户')
            ->description('列表')
            ->body($box->render());
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->id('Id');
        $show->name('Name');
        $show->phone('Phone');
        $show->email('Email');
        $show->email_verified_at('Email verified at');
        $show->password('Password');
        $show->remember_token('Remember token');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User);

        $form->hidden('register_code')->default($this->get_code());
        $form->text('name', '昵称');
        $form->mobile('phone', '手机号');
        $states = [
            'on' => ['value' => 1, 'text' => '正常', 'color' => 'success'],
            'off' => ['value' => 2, 'text' => '禁用', 'color' => 'danger'],
        ];
        $form->switch('status', '状态')->states($states);
        $form->email('email', '邮箱');
        $form->password('password', '登录密码');
        $form->password('sec_password', '交易密码');

        if (empty($form->model()->password)){
            if ($form->password) {
                $form->password = bcrypt($form->password);
            }else{
                $form->password = bcrypt('abcabc');
            }
            if ($form->sec_password) {
                $form->sec_password = bcrypt($form->sec_password);
            }else{
                $form->sec_password = bcrypt('abcabc');
            }
        }else{
            $form->password = $form->model()->password;
            $form->sec_password = $form->model()->sec_password;
        }
        
        // $form->pid = User::where('phone',$form->pid)->value('id');
        return $form;
    }
    //获取唯一邀请码
    protected function get_code()
    {
        $str = str_random(6);
        $count = User::where('register_code',$str)->count();
        if ($count > 0) {
            $this->get_code();
        }else{
            return strtolower($str);
        }
    }
}
